package com.calvin.study.completablefuture;

import java.math.BigDecimal;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.RandomUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

public class BaseThreadPoolExecutor {

	static ThreadPoolExecutor executor = null;
	
	private static Order order = null;
	
	
	static {
		if (null == executor) {
			executor = new ThreadPoolExecutor(5, 5, 10L, TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>(3),
					Executors.defaultThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy());
		}
	}

	
	public static Order getInsatance() {
		if(null == order) {
			order = new Order();
			order.setOrderId(new Snowflake().nextIdStr());
			order.setPrice(RandomUtil.randomBigDecimal(new BigDecimal(10), new BigDecimal(101)));
			order.setCount(RandomUtil.randomInt(1, 21));
			order.setTotalPrice(order.getPrice().multiply(new BigDecimal(order.getCount())));
		}
		return order;
	}

	
}

@Data
@AllArgsConstructor
@NoArgsConstructor
class Order{
	private String orderId;
	private int count;
	private BigDecimal price;
	private BigDecimal totalPrice;
}
